#include "../include/e_nodes.h"

e_nodes::e_nodes(e_nodes *parent, e_cube the_me)
{
    me->dimension = the_me.dimension;   me->anchor.x = the_me.anchor.x;     me->anchor.y = the_me.anchor.y;     me->anchor.z = the_me.anchor.z;
    subdivided = false; //make the node a leaf by default
}

void e_nodes::subdivide()
{
    half = me->dimension / 2.0;
    children_e_cubes = new e_cube [8];
    for(int i = 0; i < 8; i++){
        e_nodes (this, children_e_cubes[i]);
    }

    subdivided = true;
}

inline void e_nodes::setup_e_cubes()
{
    children_e_cubes[0].anchor.x = me->anchor.x;        children_e_cubes[0].anchor.y = me->anchor.y;        children_e_cubes[0].anchor.z = me->anchor.z;
    children_e_cubes[1].anchor.x = me->anchor.x;        children_e_cubes[1].anchor.y = me->anchor.y;        children_e_cubes[1].anchor.z = me->anchor.z + half;
    children_e_cubes[2].anchor.x = me->anchor.x;        children_e_cubes[2].anchor.y = me->anchor.y + half; children_e_cubes[2].anchor.z = me->anchor.z;
    children_e_cubes[3].anchor.x = me->anchor.x;        children_e_cubes[3].anchor.y = me->anchor.y + half; children_e_cubes[3].anchor.z = me->anchor.z + half;
    children_e_cubes[4].anchor.x = me->anchor.x + half; children_e_cubes[4].anchor.y = me->anchor.y;        children_e_cubes[4].anchor.z = me->anchor.z;
    children_e_cubes[5].anchor.x = me->anchor.x + half; children_e_cubes[5].anchor.y = me->anchor.y;        children_e_cubes[5].anchor.z = me->anchor.z + half;
    children_e_cubes[6].anchor.x = me->anchor.x + half; children_e_cubes[6].anchor.y = me->anchor.y + half; children_e_cubes[6].anchor.z = me->anchor.z;
    children_e_cubes[7].anchor.x = me->anchor.x + half; children_e_cubes[7].anchor.y = me->anchor.y + half; children_e_cubes[7].anchor.z = me->anchor.z + half;

    for(int i = 0; i < 8; i++){
        children_e_cubes[i].dimension = half;
    }
}

void e_nodes::sides()
{
    const GLfloat position_data[72] = {
        me->anchor.x,                   me->anchor.y,                   me->anchor.z,
        me->anchor.x + me->dimension,   me->anchor.y,                   me->anchor.z,
        me->anchor.x + me->dimension,   me->anchor.y + me->dimension,   me->anchor.z,
        me->anchor.x,                   me->anchor.y + me->dimension,   me->anchor.z,

        me->anchor.x,                   me->anchor.y,                   me->anchor.z,
        me->anchor.x + me->dimension,   me->anchor.y,                   me->anchor.z + me->dimension,
        me->anchor.x + me->dimension,   me->anchor.y,                   me->anchor.z + me->dimension,
        me->anchor.x,                   me->anchor.y,                   me->anchor.z,

        me->anchor.x,                   me->anchor.y,                   me->anchor.z,
        me->anchor.x,                   me->anchor.y,                   me->anchor.z + me->dimension,
        me->anchor.x,                   me->anchor.y + me->dimension,   me->anchor.z + me->dimension,
        me->anchor.x,                   me->anchor.y + me->dimension,   me->anchor.z,

        me->anchor.x + me->dimension,   me->anchor.y + me->dimension,   me->anchor.z + me->dimension,
        me->anchor.x,                   me->anchor.y + me->dimension,   me->anchor.z + me->dimension,
        me->anchor.x,                   me->anchor.y,                   me->anchor.z + me->dimension,
        me->anchor.x + me->dimension,   me->anchor.y,                   me->anchor.z + me->dimension,

        me->anchor.x + me->dimension,   me->anchor.y + me->dimension,   me->anchor.z + me->dimension,
        me->anchor.x,                   me->anchor.y + me->dimension,   me->anchor.z,
        me->anchor.x,                   me->anchor.y + me->dimension,   me->anchor.z,
        me->anchor.x + me->dimension,   me->anchor.y + me->dimension,   me->anchor.z + me->dimension,

        me->anchor.x + me->dimension,   me->anchor.y + me->dimension,   me->anchor.z + me->dimension,
        me->anchor.x + me->dimension,   me->anchor.y + me->dimension,   me->anchor.z,
        me->anchor.x + me->dimension,   me->anchor.y,                   me->anchor.z,
        me->anchor.x + me->dimension,   me->anchor.y,                   me->anchor.z + me->dimension
    };
    position_data_ptr = const_cast<GLfloat *>(&position_data[0]);
}

void e_nodes::show()
{
    if(subdivided) return;
    if(position_data_ptr == NULL){
        this->sides();
    }
    glEnableClientState(GL_VERTEX_ARRAY);
    glVertexPointer(3, GL_FLOAT, 0, position_data_ptr);
    glDrawArrays(GL_QUADS, 0, 24);
    glDisableClientState(GL_VERTEX_ARRAY);
}
